package com.qh.resourcehub.utils;


import com.qh.resourcehub.config.TJRoleConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class XuguUtils {

    @Resource
    TJRoleConfig tjRoleConfig;

    public Connection getConnection() {

        String ip = tjRoleConfig.getXuguIp();
        int port = tjRoleConfig.getXuguPort();
        String library = tjRoleConfig.getXuguLibrary();
        String username = tjRoleConfig.getXuguUsername();
        String password = tjRoleConfig.getXuguPassword();

        Connection conn = null;
        String jdbcurl = "jdbc:xugu://" + ip + ":" + port + "/" + library;
        System.out.println(jdbcurl);
        //   jdbc:xugu://10.155.90.202:36546/BEHK_DP   sunsheen     Sunsheen@100200
        try {
            Class.forName("com.xugu.cloudjdbc.Driver");
            try {
                conn = DriverManager.getConnection(jdbcurl, username, password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @param ip
     * @param port
     * @param library  库
     * @param username
     * @param password
     * @return
     */
    public static Connection getConnection(String ip, int port, String library, String username, String password) {
        Connection conn = null;
        String jdbcurl = "jdbc:xugu://" + ip + ":" + port + "/" + library;
        //   jdbc:xugu://10.155.90.202:36546/BEHK_DP   sunsheen     Sunsheen@100200
        try {
            Class.forName("com.xugu.cloudjdbc.Driver");
            try {
                conn = DriverManager.getConnection(jdbcurl, username, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public List<LinkedHashMap<String, Object>> getDataMap(Connection connection, String sql) {
        Statement statement = null;
        List<LinkedHashMap<String, Object>> list =new ArrayList<>();
        try {
            statement = connection.createStatement();
            ResultSet result = statement.executeQuery(sql);
            ResultSetMetaData metaData = result.getMetaData();
            while (result.next()) {
                LinkedHashMap<String, Object> rowData = new LinkedHashMap<>();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnLabel(i);
                    String columnValue = result.getString(i);
                    rowData.put(columnName, columnValue);
                }
                list.add(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            connection.close();
            statement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
    public int executeUpdate(Connection connection, String sql) {
        Statement statement = null;
        int result = 0;
        try {
            statement = connection.createStatement();
            result = statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            // 在这里可以处理失败的情况，比如记录日志、返回特定的错误码等
            return -1; // 返回-1表示操作失败
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
//                if (connection != null) {
//                    connection.close();
//                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result; // 返回受影响的行数，0表示没有行被影响
    }
}
